using System.Linq;
using Core;
using IglooCoder.Commons.WcfNhibernate;
using NHibernate;
using NHibernate.Linq;

namespace Infrastucture
{
    public class InventoryAdjustmentRepository : BaseNHibernateRepository<InventoryAdjustment>
    {
        public InventoryAdjustmentRepository(ISessionStorage sessionStorage)
            : base(sessionStorage)
        {
        }


        public string GetMaxNumber(string kodeAwal)
        {
            string res = string.Empty;
            var lenKode = kodeAwal.Length + 10;
            var query = Session.CreateSQLQuery("Select Max(NoAdj) as Number from InventoryAdjustment where NoAdj like'" + kodeAwal + "%' AND Len(NoAdj)=" + lenKode).AddScalar("Number", NHibernateUtil.String);
            var objRes = query.List()[0];
            if (objRes != null) res = objRes.ToString();
            return res;
        }

        public void UpdateStatus(string NoAdj, int Status)
        {
            ISQLQuery command =
            Session.CreateSQLQuery("Update InventoryAdjustment set Status=" + Status + " Where NoAdj='" + NoAdj + "'");
            int res = command.ExecuteUpdate();
        }

        public void UpdateEmailStatus(string NoAdj)
        {
            ISQLQuery command =
            Session.CreateSQLQuery("Update InventoryAdjustment set HasSent=1 Where NoAdj='" + NoAdj + "'");
            int res = command.ExecuteUpdate();
        }
    }
}